package com.jeasonzhao.commons.excel;

import java.lang.reflect.Field;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class Row
{
    private HSSFRow m_row = null;
    private Sheet m_sheet = null;
    private WorkBook m_workBook = null;
    public Row(WorkBook workBook,Sheet sheet,HSSFRow row)
    {
        m_row = row;
        m_workBook = workBook;
        m_sheet = sheet;
    }

    public HSSFRow getHSSFRow()
    {
        return m_row;
    }

    public String getSheetName()
    {
        return m_sheet.getName();
    }

    public Sheet getSheet()
    {
        return m_sheet;
    }

    public int hashCode()
    {
        return super.hashCode();
    }

    public ArrayList<Cell> cells()
    {
        ArrayList<Cell> ret = new ArrayList<Cell>();
        int nLastIndex = this.getLastCellNum();
        for(int n = 0;n <= nLastIndex;n++)
        {
            ret.add(this.cell(n));
        }
        return ret;
    }

    public void removeCell(Cell cell)
    {
        if(cell == null)
        {
            return;
        }
        try
        {
            Field f = Cell.class.getDeclaredField("m_cell");
            f.setAccessible(true);
            HSSFCell c = (HSSFCell) f.get(cell);
            if(null != c)
            {
                m_row.removeCell(c);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }

    public void setRowNum(int rowNum)
    {
        m_row.setRowNum(rowNum);
    }

    public int getRowNum()
    {
        return m_row.getRowNum();
    }

    public Object[] values()
    {
        int nLastIndex = this.getLastCellNum();
        Object[] ret = new Object[nLastIndex + 1];
        for(int n = 0;n <= nLastIndex;n++)
        {
            Cell cell = this.cell(n);
            ret[n] = cell == null ? null : cell.getObject();
        }
        return ret;
    }

    public Cell cellEx(int cellnum)
    {
        HSSFCell hssfcell = this.m_row.getCell(cellnum);
        if(null == hssfcell)
        {
            hssfcell = this.m_row.createCell(cellnum);
        }
        return new Cell(this.m_workBook,this.m_sheet,this,hssfcell);
    }

    public Cell cell(int cellnum)
    {
        HSSFCell hssfcell = this.m_row.getCell(cellnum);
        if(null == hssfcell)
        {
            return null;
        }
        else
        {
            return new Cell(this.m_workBook,this.m_sheet,this,hssfcell);
        }
    }

    public Row setCellValue(int n,Object obj)
    {
        this.cellEx(n).setValue(obj);
        return this;
    }

    public String getCellValue(int nCell)
    {
        Cell cell = this.cell(nCell);
        if(null == cell)
        {
            return null;
        }
        else
        {
            return cell.getString();
        }
    }

    public int getFirstCellNum()
    {
        return m_row.getFirstCellNum();
    }

    public int getLastCellNum()
    {
        return m_row.getLastCellNum();
    }

    public int getPhysicalNumberOfCells()
    {
        return m_row.getPhysicalNumberOfCells();
    }

    public void setHeight(short height)
    {
        m_row.setHeight(height);
    }

    public void setHeightInPoints(float height)
    {
        m_row.setHeightInPoints(height);
    }

    public short getHeight()
    {
        return m_row.getHeight();
    }

    public float getHeightInPoints()
    {
        return m_row.getHeightInPoints();
    }

    public int compareTo(Object obj)
    {
        return m_row.compareTo(obj);
    }

    //Do not allow two rows compare
    public boolean equals(Object obj)
    {
        return super.equals(obj);
    }

    public String toHTML(int nMaxColumns,boolean isAddTr)
    {
//        nMaxColumns = nMaxColumns < 0 ? 255 : nMaxColumns;
//        Sheet sheet = new Sheet(this.m_workBook,this.m_sheet,"");
//        StringBuffer line = new StringBuffer("");
//        int nRow = this.getRowNum();
//        for(int nCell = 0;nCell < nMaxColumns && nCell < getLastCellNum();nCell++)
//        {
//            Cell cellInstance = this.getOrCreate(nCell);
//            String strColSpanRowSpan = "";
//
//            Region region = sheet.getRegionFromTopLeft(nRow,nCell);
//            int nColSpan = 0;
//            int nRowSpan = 0;
//            if(null != region)
//            {
//                nColSpan = region.getColumnTo() - region.getColumnFrom() + 1;
//                nRowSpan = region.getRowTo() - region.getRowFrom() + 1;
//                strColSpanRowSpan = "";
//                if(nColSpan > 1)
//                {
//                    strColSpanRowSpan += " colspan=\"" + nColSpan + "\"";
//                }
//                if(nRowSpan > 1)
//                {
//                    strColSpanRowSpan += " rowspan=\"" + nRowSpan + "\"";
//                }
//            }
//            else if(sheet.getTopRegion(nRow,nCell) != null)
//            {
//                continue;
//            }
////            String cBack = ExcelHelper.getCellBackgroundColorInHtml(m_palette,cell);
//            line.append("<td " + strColSpanRowSpan);
////                int nPixelWidth=(int)(sheet.getColumnWidth(nCell)/32);
////               //System.out.println(nCell+"  "+sheet.getColumnWidth(nCell));
////               if(nPixelWidth<10)
////               {
////                   nPixelWidth= nDefaultColumnPixel;
////               }
////                if(nPixelWidth>0 && nRowSpan<2&&nColSpan<2)
////                {
////                    line.append(" width='"+nPixelWidth+"px'");
////                }
////                if(null != cBack)
////                {
////                    line.append(" bgcolor=#" + cBack);
////                }
//            if(cellInstance.getCellStyle().getAlignment().equals(TextAlign.RIGHT))
//            {
//                line.append(" align=right");
//            }
//            else if(cellInstance.getCellStyle().getAlignment().equals(TextAlign.CENTER))
//            {
//                line.append(" align=center");
//            }
////                if(cellInstance.getCellStyle().getWrapText()  ==false)
//            {
//                line.append(" nowrap");
//            }
//            String strCellValue = cellInstance.getStringCellValue();
////                strCellValue = ExcelHelper.convertString2Html(strCellValue);
////                HSSFFont font = m_workBook.getFontAt(cell.getCellStyle().getFontIndex());
////                if(null != font)
//            {
////                    if(font.getBoldweight() == font.BOLDWEIGHT_BOLD)
////                    {
////                        strCellValue = "<b>" + strCellValue + "</b>";
////                    }
////                    String cFore = ExcelColorHelper.getHex(font.getColor());
////                    if(null == cFore)
////                    {
////                        cFore = ExcelColorHelper.getHex(m_palette == null ? null :
////                            m_palette.getColor(font.getColor()));
////                    }
////                    if(null != cFore)
////                    {
////                        strCellValue = "<font style='font-size:" + font.getFontHeightInPoints() + "pt' face='" + font.getFontName() + "' color=#" + cFore + ">" + strCellValue + "</font>";
////                    }
//            }
//            strCellValue = strCellValue.replaceAll("\\\r","").replaceAll("\\\n","<br />");
//            line.append(">");
//            line.append(strCellValue + "</td>");
//            //System.out.println(line);
//        }
//        if(isAddTr)
//        {
//            return "<tr>" + line.toString() + "</tr>";
//        }
//        else
//        {
//            return line.toString();
//        }
        /**
         * @todo asd
         */
        return null;
    }

    public Row applyCellStyles(CellStyle style)
    {
        for(int n = this.getFirstCellNum();n < 255 && n <= this.getLastCellNum();n++)
        {
            Cell cell = this.cell(n);
            if(null != cell)
            {
                cell.setCellStyle(style);
            }
        }
        return this;
    }

    public Row applyCellStyles(int nStart,int nEnd,CellStyle style)
    {
        for(int n = nStart;n <= this.getLastCellNum() && n <= nEnd;n++)
        {
            Cell cell = this.cell(n);
            if(null != cell)
            {
                cell.setCellStyle(style);
            }
        }
        return this;
    }

    public Row mergeCells(int nStart,int nEnd)
    {
        if(nStart < 0 || nEnd < 0 || nStart >= nEnd)
        {
            return this;
        }
        this.m_sheet.mergeCells(this.getRowNum(),nStart,this.getRowNum(),nEnd);
        return this;
    }

    public Row setValues(Object ...values)
    {
        if(null == values)
        {
            return this;
        }
        for(int n = 0;n < values.length;n++)
        {
            this.setCellValue(n,values[n]);
        }
        return this;
    }

    public void remove()
    {
        if(null != this.m_sheet && null != this.m_row)
        {
            this.m_sheet.getHSSFSheet().removeRow(this.m_row);
        }
    }
}
